<template>
    <van-nav-bar title="搜索" fixed left-text="返回" left-arrow @click-left="() => { $router.push('/') }" />
    <div style="height: 50px;"></div>
    <van-search v-model="searchVal" shape="round" background="pink" placeholder="请输入搜索关键词" @click="search" />
    <!-- 没数据 -->
    <template v-if="searchVal.length == 0 || searchData.length === 0">
        <van-empty description="描述文字" />
    </template>
    <!-- 有数据 -->
    <template v-else>
        <div v-for="item in searchData">
            <router-link :to="'/detail/' + item.goods_id">
                <van-cell-group>
                    <van-cell :title="item.goods_name" />
                </van-cell-group>
            </router-link>
        </div>


    </template>
</template>

<script setup>
import { ref, watch } from 'vue'
import { searchApi } from '@/api/api'
let searchVal = ref('')
let searchData = ref([])
import _ from 'lodash'
watch(searchVal, _.debounce((searchVal) => {
    searchApi(searchVal).then(res => {
        console.log('搜素数据', res)
        searchData.value = res.data.message
    })
}))
</script>

<style lang="scss" scoped></style>